/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aburakc.network;

import aburakc.MainMIDlet;
import aburakc.message.SMSTextMessage;
import aburakc.sender.SMSSender;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

/**
 *
 * @author burak.costu
 */
public class SMSNetworkListener implements Runnable {

    private static SMSNetworkListener instance;
    private static Thread thread;
    private String address;
    private boolean destroy = false;
    private MainMIDlet mainMIDlet;
     /**
     * @return the mainMIDlet
     */
    public MainMIDlet getMainMIDlet() {
        return mainMIDlet;
    }

    /**
     * @param mainMIDlet the mainMIDlet to set
     */
    public void setMainMIDlet(MainMIDlet mainMIDlet) {
        this.mainMIDlet = mainMIDlet;
    }
    private SMSNetworkListener() {
    }

    public static SMSNetworkListener getInstance() {
        if (instance == null) {
            instance = new SMSNetworkListener();
            thread = new Thread(instance);
            thread.start();
        }

        return instance;
    }

    public void destroy() {
        setDestroy(true);
    }

    public void run() {
        HttpConnection connection = null;
        InputStream inputstream = null;
        String str = "";
        while (!isDestroy()) {
            try {
                str = "";
                connection = (HttpConnection) Connector.open("http://localhost:8888/smsGet");
                //HTTP Request
                connection.setRequestMethod(HttpConnection.GET);
                connection.setRequestProperty("Content-Type", "text/html");
                connection.setRequestProperty("Connection", "close");
                
                if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
                    
                    inputstream = connection.openInputStream();
                    int length = (int) connection.getLength();
                    if (length != -1) {
                        byte incomingData[] = new byte[length];
                        inputstream.read(incomingData);
                        str = new String(incomingData);
                    } else {
                        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
                        int ch;
                        while ((ch = inputstream.read()) != -1) {
                            bytestream.write(ch);
                        }
                        str = new String(bytestream.toByteArray());
                        bytestream.close();
                    }
                }
            } catch (IOException ex) {
                ex.printStackTrace();

            } finally {
                try {
                    inputstream.close();
                    connection.close();
                } catch (Exception ex) {
                    //ex.printStackTrace();
                }
            }
            System.out.println(str);
            if (str.length() > 2) {
                SMSTextMessage sms = MessageProcessor.toSmsTextMessage(str);
                mainMIDlet.addScreenMessage(sms.getMessageId() +" net message received");
                SMSSender.getInstance().sendSms(sms);
            }

            try {
                Thread.sleep(30000);
            } catch (InterruptedException ex) {
               // ex.printStackTrace();

            }

        }

    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the destroy
     */
    public boolean isDestroy() {
        return destroy;
    }

    /**
     * @param destroy the destroy to set
     */
    public void setDestroy(boolean destroy) {
        this.destroy = destroy;
    }

   
}
